home *** CD-ROM | disk | FTP | other *** search
/ CD Concept 6 / CD Concept 06.iso / mac / UTILITAIRE / RLaB / rlib / cumprod.r < prev    next >
Text File  |  1994-09-23  |  697b  |  40 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    cumprod ( X )
  4.  
  5. //  Description:
  6.  
  7. //  Compute the cumulative product of a vector or a matrix.
  8.  
  9. //  See Also: prod
  10.  
  11. //-------------------------------------------------------------------//
  12.  
  13. cumprod = function ( x )
  14. {
  15.   m = x.nr;
  16.   n = x.nc;
  17.  
  18.   new = zeros (m, n);
  19.   if (min ([m, n]) == 1)
  20.   {
  21.     // cumsum on a vector
  22.     new[1] = x[1];
  23.     for (i in 2:max ([m,n]))
  24.     {
  25.       new[i] = x[i] * new[i-1];
  26.     }
  27.   else
  28.     // cumsum on the columns of a matrix
  29.     for (i in 1:n)
  30.     {
  31.       new[1;i] = x[1;i];
  32.       for (j in 2:m)
  33.       {
  34.         new[j;i] = x[j;i] * new[j-1;i];
  35.       }
  36.     }
  37.   }
  38.   return (new);
  39. };
  40.